<template>
	<view class="content">
		<view class="title">
			<p class="title-text">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题：</p>
			<view class="title-content">
				<textarea class="title-textarea" v-model="title" placeholder="请输入标题" type="text" auto-height></textarea>
			</view>
		</view>
		<view class="addcontent">
			<p class="addcontent-text">计划内容：</p>
			<view class="addcontent-concent">
				<textarea class="addcontent-textarea" v-model="content" placeholder="请输入内容" type="text"
					auto-height></textarea>
			</view>
		</view>
		<view class="example">
			<view class="example-text">
				开始时间：
			</view>
			<view class="example-body">
				<uni-datetime-picker type="datetime" v-model="date1" @change="handleChange1" />
			</view>
		</view>
		<view class="example">
			<view class="example-text">
				结束时间：
			</view>
			<view class="example-body">
				<uni-datetime-picker type="datetime" v-model="date2" @change="handleChange2" />
			</view>
		</view>
		<view class="addplan">
			<button class="addplan-button" @click="addPlan()">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				content: '',
				date1: null,
				date2: null,
				teacherId: null
			}
		},
		mounted() {
			setTimeout(() => {
				this.datetimesingle = "2021-5-1";
				this.single = "2021-5-1";
			}, 1000);
			this.getId();
		},
		methods: {
			handleChange1(e) {
				// e.detail.value 是新的日期时间值  
				this.date1 = e.detail.value;
				// 在这里可以添加其他处理逻辑  
			},
			// 第二个日期时间输入框的变化处理函数  
			handleChange2(e) {
				// e.detail.value 是新的日期时间值  
				this.date2 = e.detail.value;
				// 在这里可以添加其他处理逻辑  
			},
			goPage(id) {
				console.log(id);
				if (id == 0) {
					uni.switchTab({
						url: '/pages/login/login'
					})
				} else if (id >= 1) {
					uni.navigateTo({
						url: '/pages/zuoyedeta/zuoyedeta?id=' + encodeURIComponent(id.toString())
					})
				} else {
					uni.navigateTo({
						url: '/pages/jindu/jindu'
					})
				}
			},
			onPullDownRefresh() {
				console.log('刷新页面');
				setTimeout(() => {
					uni.stopPullDownRefresh();
				}, 1000)
			},
			getJwtToken() {
				try {
					return wx.getStorageSync('jwt_token');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 假设你使用 uni.request 发送请求  
			sendWithToken(url, method) {
				const token = this.getJwtToken();
				if (token) {
					wx.request({
						url: url,
						method: method,
						header: {
							'token': token // 将令牌添加到 Authorization 头部  
						},
						success: (res) => {
							if (res.data.code === 1 && res.data.msg === 'success') {

								this.teacherId = res.data.data;

							} else if (res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: null
								});
								this.goPage(0);
								console.error('未登录', res.data.data);
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '网络请求失败',
								icon: 'none'
							});
							console.error('请求失败', error);
						}
					});
				} else {
					this.goPage(0);
					// 如果没有令牌，处理没有令牌的情况，比如跳转到登录页  
					console.error('No token found, please login first.');
				}
			},
			
			addWithToken(url, method, data) {
				const token = this.getJwtToken();
				if (token) {
					wx.request({
						url: url,
						method: method,
						data: data,
						header: {
							'token': token // 将令牌添加到 Authorization 头部  
						},
						success: (res) => {
							if (res.data.code === 1 && res.data.msg === 'success') {
								console.log('添加成功')
								uni.navigateBack({  
								    delta: 1 // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。  
								});
							} else if (res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: null
								});
								this.goPage(0);
								console.error('未登录', res.data.data);
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '网络请求失败',
								icon: 'none'
							});
							console.error('请求失败', error);
						}
					});
				} else {
					this.goPage(0);
					// 如果没有令牌，处理没有令牌的情况，比如跳转到登录页  
					console.error('No token found, please login first.');
				}
			},
			getuserName() {
				try {
					return wx.getStorageSync('user_name');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 在某个页面的事件处理函数中  
			getId() {
				const username = this.getuserName();
				if (username) {
					const url = `http://localhost:8080/teacher/getId/${username}`;
					const method = 'GET';
					this.sendWithToken(url, method);
				} else {
					console.error("用户名获取失败")
				}
			},
			addPlan() {

				this.date1 = this.date1.replace(' ', 'T');
				this.date2 = this.date2.replace(' ', 'T');
				const addData = {
					title: this.title,
					content: this.content,
					rekedateTime: this.date1,
					expirdateTime: this.date2,
					teacherId: this.teacherId
				}
				const url = `http://localhost:8080/teacher/progress`;
				const method = 'POST';
				this.addWithToken(url, method, addData);
			},
			onload() {

			}
		}
	};
</script>
<style>
	.content {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		padding-top: 100rpx;
		width: 95%;
		height: auto;
	}

	.title {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 95%;
		height: auto;
		padding: 0%;
		margin: 50rpx 0rpx;

	}

	.title-text {
		display: flex;
		align-items: center;
		width: 25%;
		height: 80rpx;
		font-family: AliMamaShuHei;
		font-size: 30rpx;
		margin: 0%;

	}

	.title-content {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 75%;
		height: auto;
		border-bottom-width: 1px;
		border-bottom-color: #E0E0E0;
	}

	.title-textarea {
		display: flex;
		align-items: center;
		width: 100%;
		height: auto;
		word-wrap: break-all;
		padding-bottom: 20rpx;
	}

	.addcontent {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 95%;
		height: auto;
		margin: 50rpx 0rpx;

	}

	.addcontent-text {
		display: flex;
		align-items: center;
		width: 25%;
		height: 80rpx;
		font-family: AliMamaShuHei;
		font-size: 30rpx;
		margin: 0%;

	}

	.addcontent-concent {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 75%;
		height: auto;
		border-bottom-width: 1px;
		border-bottom-color: #E0E0E0;
	}

	.addcontent-textarea {
		display: flex;
		align-items: center;
		width: 100%;
		height: auto;
		word-wrap: break-all;
		padding-bottom: 20rpx;

	}

	.example {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 95%;
		height: auto;
		margin: 50rpx 0rpx;
		margin-bottom: 100rpx;
	}

	.example-text {
		display: flex;
		align-items: center;
		width: 25%;
		height: 80rpx;
		font-family: AliMamaShuHei;
		font-size: 30rpx;

	}

	.example-body {
		display: flex;
		align-items: center;
		width: 75%;
		height: 80rpx;
		font-family: AliMamaShuHei;
		font-size: 30rpx;
	}

	.addplan-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 300rpx;
		height: 70rpx;
		border-radius: 50rpx;
		background-color: #585CE5;
		font-family: AliMamaShuHei;
		color: #F3F3F3;
		font-size: 28rpx;

	}
</style>